package com.cloudkinto.controller.wr;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.company.CompanyListVo;
import com.cloudkinto.vo.ft.FreightTempPageVo;
import com.cloudkinto.vo.wr.QsPageVo;
import com.cloudkinto.vo.ft.FreightStatusUpdateReq;
import com.cloudkinto.vo.ob.ChannelEt;
import com.cloudkinto.vo.ob.QsListEtVo;
import com.cloudkinto.vo.wr.qs.QsAddReq;
import com.cloudkinto.vo.wr.qs.QsDetailRes;
import com.cloudkinto.vo.wr.qs.QsUpdateReq;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.wr.QuotationSchemeService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2024-11-15
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "总报价方案omp模块")
@RequestMapping("/api/admin/omp/QuotationScheme")
public class QuotationSchemeOmpController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(QuotationSchemeOmpController.class);

    private final QuotationSchemeService service;


    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated QsAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功",service.add(req,userId, getTenantId()));
    }


    @UserSessionRequired
    @PutMapping("/updateStatus")
    @ApiOperation(value = "报价方案审核废弃", notes = "报价方案审核废弃")
    public SingleResult updateStatus(@RequestBody @Validated FreightStatusUpdateReq req) {
        Long userId = getCurrentUserId();
        service.updateStatus(req, userId, getTenantId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated QsUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功",service.update(req,userId, getTenantId()));
    }


    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id, userId, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = QsDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = QsPageVo.class))
    public SingleResult list(HttpServletRequest request){
        Map<String, Object> map = this.getMap(request);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "quotationType", value = "类型"),
            @ApiImplicitParam(name = "currencyCode", value = "币种"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "storageIds", value = "仓库", type = "arr"),
            @ApiImplicitParam(name = "customerIds", value = "客户", type = "arr"),
            @ApiImplicitParam(name = "quotationName", value = "报价名称"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = QsPageVo.class))
    public PageResult listPage(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    public Map<String, Object> getMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("source", "omp");
        map.put("userId", getCurrentUserId());

        String quotationType = request.getParameter("quotationType");
        String currencyCode = request.getParameter("currencyCode");
        String storageIds = request.getParameter("storageIds");
        String status = request.getParameter("status");
        String customerIds = request.getParameter("customerIds");

        String quotationName = request.getParameter("quotationName");
        map.put("quotationType", quotationType);
        map.put("currencyCode", currencyCode);
        map.put("status", status);
        if (StringUtils.isNotBlank(storageIds)) {
            map.put("storageIds", Arrays.asList(storageIds.split(",")));
        }
        if (StringUtils.isNotBlank(customerIds)) {
            map.put("customerIds", Arrays.asList(customerIds.split(",")));
        }
        map.put("quotationName", quotationName);
        map.put("tenantId", getTenantId());
        return map;
    }



    @UserSessionRequired
    @GetMapping("/listCustomerByWhCode")
    @ApiOperation(value = "客户下拉框", notes = "客户下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "quotationId", value = "报价方案id"),
            @ApiImplicitParam(name = "quotationType", value = "类型 应收应付"),
            @ApiImplicitParam(name = "storageId", value = "仓库"),
            @ApiImplicitParam(name = "invalidTime", value = "失效时间"),
            @ApiImplicitParam(name = "effectiveTime", value = "生效时间")})
    @ApiResponses(@ApiResponse(code = 200, message = "", response = CompanyListVo.class))
    public SingleResult listCustomerByWhCode(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        String quotationId = request.getParameter("quotationId");
        String quotationType = request.getParameter("quotationType");
        String storageId = request.getParameter("storageId");
        String invalidTime = request.getParameter("invalidTime");
        String effectiveTime = request.getParameter("effectiveTime");
        map.put("tenantId", getTenantId());
        map.put("quotationId", quotationId);
        map.put("quotationType", quotationType);
        map.put("storageId", storageId);
        map.put("invalidTime", invalidTime);
        map.put("effectiveTime", effectiveTime);



        return SingleResult.success("查询成功", service.listCustomerByWhCode(map, getCurrentUserId(), "omp"));
    }


    @UserSessionRequired
    @GetMapping("/channelList")
    @ApiOperation(value = "新增编辑报价 - 物流渠道下拉框", notes = "物流渠道下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storageId", value = "仓库"),
           })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ChannelEt.class))
    public SingleResult channelList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        String storageId = request.getParameter("storageId");
        map.put("tenantId", getTenantId());
        map.put("storageId", storageId);
        return SingleResult.success("查询成功", service.channelList(map));
    }


    @UserSessionRequired
    @GetMapping("/freightList")
    @ApiOperation(value = "物流费下拉框", notes = "物流费下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelId", value = "物流渠道id"),
            @ApiImplicitParam(name = "currencyCode", value = "币种"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FreightTempPageVo.class))
    public SingleResult freightList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        String channelId = request.getParameter("channelId");
        String currencyCode = request.getParameter("currencyCode");
        map.put("currencyCode", currencyCode);
        map.put("channelId", channelId);
        return SingleResult.success("查询成功", service.freightList(map));
    }



    @UserSessionRequired
    @GetMapping("/operateBillItemList")
    @ApiOperation(value = "操作费全列表", notes = "操作费全列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currencyCode", value = "币种"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = QsListEtVo.class))
    public SingleResult operateBillItemList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        String currencyCode = request.getParameter("currencyCode");
        map.put("currencyCode", currencyCode);
        return SingleResult.success("查询成功", service.operateBillItemList(map));
    }


    @UserSessionRequired
    @GetMapping("/warehouseRentList")
    @ApiOperation(value = "仓租下拉框", notes = "仓租下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rentType", value = "币种"),
            @ApiImplicitParam(name = "currencyCode", value = "币种"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = QsListEtVo.class))
    public SingleResult warehouseRentList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        String currencyCode = request.getParameter("currencyCode");
        String rentType = request.getParameter("rentType");
        map.put("rentType", rentType);
        map.put("currencyCode", currencyCode);
        return SingleResult.success("查询成功", service.warehouseRentList(map));
    }


    @UserSessionRequired
    @GetMapping("/relatedQsList")
    @ApiOperation(value = "关联的报价方案", notes = "关联的报价方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thirdId", value = "物流费id 操作费id 仓租费id"),
            @ApiImplicitParam(name = "quotationItemType", value = "0 物流费 1操作费 2 仓租费"),
            @ApiImplicitParam(name = "quotationType", value = "报价类型 0应收 1应付")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = QsPageVo.class))
    public SingleResult relatedQsList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        String thirdId = request.getParameter("thirdId");
        String quotationItemType = request.getParameter("quotationItemType");
        String quotationType = request.getParameter("quotationType");
        map.put("thirdId", thirdId);
        map.put("quotationItemType", quotationItemType);
        map.put("quotationType", quotationType);
        return SingleResult.success("查询成功", service.relatedQsList(map));
    }

}

